#!/bin/bash
#IModule

#########################
#   名称: Zenity
#   用途: 将信息通过Zenity推送给用户
#########################
if [ -z "$_Zenity_Loaded" ];then
    function _framework_resolve_dependencies()
    {
        true;
    }

    function _Zenity_Info()
    {
        local msg="${1:-"$(_Locale_get _Generic_NoMsg)"}";
        zenity --info --ellipsize --text="$msg" 2>/dev/null;
    }

    function _Zenity_Error()
    {
        local msg="${1:-"$(_Locale_get _Generic_NoMsg)"}";
        zenity --error --ellipsize --text="$msg" 2>/dev/null;
    }

    function _Zenity_Warn()
    {
        local msg="${1:-"$(_Locale_get _Generic_NoMsg)"}";
        zenity --warning --ellipsize --text="$msg" 2>/dev/null;
    }

    function _Zenity_Question()
    {
        local msg="${1:-"$(_Locale_get _Generic_NoMsg)"}";
        zenity --question --ellipsize --text="$msg" 2>/dev/null;
        return $?;
    }

    function _Zenity_FileSelection()
    {
        local Title="${1:-"$(_Locale_get _Generic_NoMsg)"}";
        local MimeType="$2"

        local _Zenity_FileSelection_Path="";

        for ((re=1;re<=3;re++));do
        {
            if ! _Zenity_FileSelection_Path="$(zenity --file-selection --title="$(_Locale_get "$Title" "$re")" 2>/dev/null)";then
            {
                _Zenity_Error "$(_Locale_get _Zenity_InvaildPath "$re")";
            };else
            {
                #MIME检查
                if [ -n "$MimeType" ] \
                    && [ "$(file -b --mime-type "$_Zenity_FileSelection_Path")" != "$MimeType" ];then
                {
                    _Zenity_Error "$(_Locale_get _Zenity_MIMEMismatch)" >&2;
                    continue;
                };else
                {
                    echo "$_Zenity_FileSelection_Path";
                    break;
                };fi;
            };fi;

            if [ "$re" -eq 3 ];then
            {
                _Logger_Log Error "[ZenityPush] $(_Locale_get _Zenity_InvaildValueForThreeTimes)"
                return 1;
            };fi;
        };done

        return 0;
    }

    function _Zenity_DirectorySelection()
    {
        local Title="${1:-"$(_Locale_get _Generic_NoMsg)"}";
        local _Zenity_DirectorySelection_Path;

        for ((re=1;re<=3;re++));do
        {
            if ! _Zenity_DirectorySelection_Path="$(zenity --directory --file-selection --title="$(_Locale_get _Zenity_DirectorySelectTitle "$Title" "$re")" 2>/dev/null)";then
            {
                _Zenity_Error "$(_Locale_get _Zenity_InvaildPath "$re")";
            };else
            {
                echo "$_Zenity_DirectorySelection_Path";
                break;
            };fi;

            if [ "$re" -eq 3 ];then
            {
                _Logger_Log Error "[ZenityPush] $(_Locale_get _Zenity_InvaildValueForThreeTimes)"
                return 1;
            };fi;
        };done

        return 0;
    }
    
    function _Zenity_Entry()
    {
        local Title="${1:-"$(_Locale_get _Generic_NoMsg)"}";
        local msg="${2:-"${Title:-"$(_Locale_get _Generic_NoMsg)"}"}";
        local DefaultText="${3}";

        local Text="";
        if ! Text="$(zenity --entry --entry-text="$DefaultText" --text="$msg" --title="$Title")";then
            echo "$DefaultText";
            return 1;
        else
            echo "$Text";
            return 0;
        fi;
    }

    readonly _Zenity_Loaded=true;
fi;